import javax.swing.JOptionPane;
/**
 * Create Personal Data Management GUI and allow the user to change authorization and to freeze an account
 * 
 *
 */

public class PersonalDataManagmentCONTROL {
	/**
	 * Allow the user to search for an account
	 */
	public PersonalDataMangementUserGui pdmug;
	/**
	 * searchu - data to search a user to change authorization.
	 */
	public String[] searchu;
	/**
	 * searchf - data to search a user to freeze. 
	 */
	public String[] searchf;
	/**
	 * resultu - data from DataBase to change authorization. 
	 */
	public String[] resultu;
	/**
	 * resultf - data from DataBase to freeze. 
	 */
	public String[] resultf;
	/**
	 * * updateu - data about the user and the new authorization.
	 */
	public String[] updateu;
	/**
	  * updatef - data about the user and new freeze status 
	 */
	public String[] updatef;
	/**
	 * temporary String
	 */
	public String tmp;
	/**
	 * isUserR true Display allow the user to change authorization and freeze status
	 */
	public Boolean isUserR;
	
/**
 * search data about all users and display it on table
 * @param a
 */
	public void lsreachUserug(Boolean a)
	{
		isUserR=a;
		searchu=new String[3];
		searchu[0]=Main.gusers.id;
		searchu[1]="searchuserug";
		String[] row;
		String[][] allData = null;
		Main.log.connection.accept("searchuseru");
		while(Main.log.flag==false);
		Main.log.flag=false;
		tmp=ChatClient.getRs();
		if(tmp.length()!=0)
		{
			row=tmp.split("\\#");
			allData=new String[row.length][];
			for(int i=0;i<row.length;i++)
					allData[i]=row[i].split("\\*");
				
		}
		pdmug=new PersonalDataMangementUserGui(allData,isUserR);
		Main.log.frame.add(pdmug);
	}
/**
 * search data about wanted user and display it on the table	
 * @param st userName
 */
	public void lsreachUseru(String  st)
	{
		searchu=new String[3];
		searchu[0]=Main.gusers.id;
		searchu[1]="searchuseru";
		searchu[2]=st;
		String[] row;
		String[][] allData = null;
		Main.log.connection.accept(searchu[1]);
		while(Main.log.flag==false);
		Main.log.flag=false;
		tmp=ChatClient.getRs();
		if(tmp.length()!=0)
		{
			row=tmp.split("\\#");
			allData=new String[row.length][];
			for(int i=0;i<row.length;i++)
					allData[i]=row[i].split("\\*");
			pdmug.setVisible(false);
			pdmug=new PersonalDataMangementUserGui(allData,isUserR);
			pdmug.setVisible(true);
			Main.log.frame.add(pdmug);
			
		}
		else
		{
			JOptionPane.showMessageDialog(null,"Can't Find Specified Username: "+st,"ERROR",JOptionPane.ERROR_MESSAGE);
			if(isUserR)
			{
				pdmug.authorozation.setEnabled(false);
				pdmug.frz.setEnabled(false);
			}
			pdmug.save.setEnabled(false);
		}
		
	}
	/**
	 * Get data about a selected user in the table
	 */
	public void lUpdateu()
	{
		if(pdmug.jTable.getSelectedRow()!=-1)
		{
			pdmug.authorozation.setEnabled(true);
			pdmug.save.setEnabled(true);
			pdmug.frz.setEnabled(true);
			int index=Integer.parseInt((String) pdmug.jTable.getValueAt(pdmug.jTable.getSelectedRow(), 7));
			pdmug.authorozation.setSelectedIndex(index-1);
			String freeze=(String) pdmug.jTable.getValueAt(pdmug.jTable.getSelectedRow(), 8);
			if(freeze.equals("yes")) pdmug.frz.setSelected(true);
			else pdmug.frz.setSelected(false);
			pdmug.jtxtUserName.setText((String) pdmug.jTable.getValueAt(pdmug.jTable.getSelectedRow(), 2));
		}
		else
		{
			pdmug.authorozation.setEnabled(false);
			pdmug.frz.setEnabled(false);
			pdmug.save.setEnabled(false);
		}
	}
	/**
	 * Update the database about  in user data 
	 * @param user user name
	 * @param aut authorization
	 * @param freeze freeze status
	 * @param row row number in the table
	 * @param id user id
	 */
	public void lUpdatef(String user,String aut,Boolean freeze,int row,String id)
	{
		updatef=new String[5];
		updatef[0]=Main.gusers.id;
		updatef[1]="updatef";
		updatef[2]=user;
		updatef[3]=aut;
		if(freeze)	updatef[4]="1";
		else updatef[4]="0";
		if(!id.equals(Main.gusers.id))
		{
			Main.log.connection.accept(updatef[1]);
			while(Main.log.flag==false);
			Main.log.flag=false;
			if(freeze)	pdmug.jTable.setValueAt("yes",row,8);
			else pdmug.jTable.setValueAt("no",row,8);
			pdmug.jTable.setValueAt(aut,row,7);
			tmp=ChatClient.getRs();
			if(tmp.equals("OK"))
				JOptionPane.showMessageDialog(null,"UPDATE completed");
			else
				JOptionPane.showMessageDialog(null,"There was a problem with update","ERROR",JOptionPane.ERROR_MESSAGE);
		}	
		else
			JOptionPane.showMessageDialog(null,"You Cannot Change Your Own Information","ERROR",JOptionPane.ERROR_MESSAGE);
	}
	/**
	 * close Personal DataMangement User GUI and go back to the main screen
	 */
	public void exit()
	{
		pdmug.setVisible(false);
		pdmug.setEnabled(false);
		Main.log.frame.setSize(450,515);
		Main.welcomeScreen.panel.setVisible(true);
		Main.welcomeScreen.panel.setEnabled(true);
	}
}
